package com.yjf.ljr.activity;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yjf.ljr.R;
import com.yjf.ljr.common.CommonData;
import com.yjf.ljr.connection.IjConnect;
import com.yjf.ljr.dataclass.PromiseInfoClass;
import com.yjf.ljr.dataclass.PromiseInfoClass.PromiseInfo;
import com.yjf.ljr.parser.RequestBuilder;
import com.yjf.ljr.parser.RequestBuilder.RequestObject;
import com.yjf.ljr.util.PicLoad;
import com.yjf.ljr.util.SPreferences;
import com.yjf.ljr.util.ImageLoader.ImageCallback;

public class RepaymentScheduleActivity extends IjActivity {
	@ID("tvCommitmentagency")
	private TextView mTvCommitmentagency;
	@ID("ivImage")
	private ImageView mIvImage;
	private ArrayList<String> mPicUrls = new ArrayList<String>();

	@ID("fail_repayment_schedule")
	private LinearLayout mLlFail_repayment_schedule;

	@ID("llrepayment_schedule")
	private LinearLayout mLlrepayment_schedule;
	private String demandId;
	private String loanStatement;// 担保机构简介
	private String guaranteeStatement;// 风控措施
	private String info3;// 涉诉信息
	private String sponsorStatement;// 担保机构意见

	@ID("tvloanStatement")
	private WebView tvloanStatement;// 担保机构简介
	@ID("tvguaranteeStatement")
	private WebView tvguaranteeStatement;// 风控措施
	@ID("tvinfo3")
	private WebView tvinfo3;// 涉诉信息
	@ID("tvsponsorStatement")
	private WebView tvsponsorStatement;// 担保机构意见

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_repayment_schedule);
		initControl();
		demandId = getIntent().getStringExtra("demandId");
		// new RepaymentScheduleActivityTask(demandId).execute();
		// showProgressDialog();
	}

	private void initControl() {
		setLeftBtnClick();
		setTitleStr("担保信息");
		loanStatement = getIntent().getStringExtra("loanStatement");// 担保机构简介
		guaranteeStatement = getIntent().getStringExtra("guaranteeStatement");// 风控措施
		info3 = getIntent().getStringExtra("projectInfo");// 涉诉信息
		sponsorStatement = getIntent().getStringExtra("sponsorStatement");// 担保机构意见
		/******************/
		WebSettings settingsloanStatement = tvloanStatement.getSettings();

		tvloanStatement.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
		settingsloanStatement.setSupportZoom(false);
		settingsloanStatement.setBuiltInZoomControls(false);
		settingsloanStatement.setDomStorageEnabled(true);
		settingsloanStatement.setAppCacheMaxSize(1024 * 1024 * 9);// 设置缓冲大小
		String appCacheDirloanStatement = mContext.getDir("cache", Context.MODE_PRIVATE).getPath();
		settingsloanStatement.setAppCachePath(appCacheDirloanStatement);
		settingsloanStatement.setAllowFileAccess(true);
		settingsloanStatement.setAppCacheEnabled(true);
		settingsloanStatement.setCacheMode(WebSettings.LOAD_DEFAULT);
		/******************/
		/******************/
		WebSettings settingsguaranteeStatement = tvguaranteeStatement.getSettings();

		tvguaranteeStatement.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
		settingsguaranteeStatement.setSupportZoom(false);
		settingsguaranteeStatement.setBuiltInZoomControls(false);
		settingsguaranteeStatement.setDomStorageEnabled(true);
		settingsguaranteeStatement.setAppCacheMaxSize(1024 * 1024 * 9);// 设置缓冲大小
		String appCacheDirguaranteeStatement = mContext.getDir("cache", Context.MODE_PRIVATE).getPath();
		settingsguaranteeStatement.setAppCachePath(appCacheDirguaranteeStatement);
		settingsguaranteeStatement.setAllowFileAccess(true);
		settingsguaranteeStatement.setAppCacheEnabled(true);
		settingsguaranteeStatement.setCacheMode(WebSettings.LOAD_DEFAULT);
		/******************/
		/******************/
		WebSettings settingsinfo3 = tvinfo3.getSettings();

		tvinfo3.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
		settingsinfo3.setSupportZoom(false);
		settingsinfo3.setBuiltInZoomControls(false);
		settingsinfo3.setDomStorageEnabled(true);
		settingsinfo3.setAppCacheMaxSize(1024 * 1024 * 9);// 设置缓冲大小
		String appCacheDirinfo3 = mContext.getDir("cache", Context.MODE_PRIVATE).getPath();
		settingsinfo3.setAppCachePath(appCacheDirinfo3);
		settingsinfo3.setAllowFileAccess(true);
		settingsinfo3.setAppCacheEnabled(true);
		settingsinfo3.setCacheMode(WebSettings.LOAD_DEFAULT);
		/******************/
		/******************/
		WebSettings settingssponsorStatement = tvsponsorStatement.getSettings();

		tvsponsorStatement.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
		settingssponsorStatement.setSupportZoom(false);
		settingssponsorStatement.setBuiltInZoomControls(false);
		settingssponsorStatement.setDomStorageEnabled(true);
		settingssponsorStatement.setAppCacheMaxSize(1024 * 1024 * 9);// 设置缓冲大小
		String appCacheDirsponsorStatement = mContext.getDir("cache", Context.MODE_PRIVATE).getPath();
		settingssponsorStatement.setAppCachePath(appCacheDirsponsorStatement);
		settingssponsorStatement.setAllowFileAccess(true);
		settingssponsorStatement.setAppCacheEnabled(true);
		settingssponsorStatement.setCacheMode(WebSettings.LOAD_DEFAULT);
		/******************/
		tvloanStatement.loadDataWithBaseURL(null, loanStatement, "text/html", "utf-8", null);// 担保机构简介
		tvguaranteeStatement.loadDataWithBaseURL(null, guaranteeStatement, "text/html", "utf-8", null);// 风控措施
		tvinfo3.loadDataWithBaseURL(null, info3, "text/html", "utf-8", null);// 涉诉信息
		tvsponsorStatement.loadDataWithBaseURL(null, sponsorStatement, "text/html", "utf-8", null);// 担保机构意见

	}

	class RepaymentScheduleActivityTask extends AsyncTask<Void, Void, Void> implements OnClickListener {
		private String projectId;// 项目ID
		private String mFailLog;
		private PromiseInfoClass date = new PromiseInfoClass();

		public RepaymentScheduleActivityTask(String projectId) {
			this.projectId = projectId;
		}

		@Override
		protected Void doInBackground(Void... arg0) {
			// TODO Auto-generated method stub
			RequestObject requestObject = new RequestObject();
			requestObject.method = "getPromiseInfo";
			requestObject.map.put("demandId", demandId);

			String requestSmsUrl = RequestBuilder.build(requestObject);
			try {
				IjConnect.doRequest(requestSmsUrl, date);
				if (date.code == null) {
					mFailLog = getResources().getString(R.string.loaddata_exception);
				} else if (!date.code.equals(CommonData.RESULT_SUCCESS)) {
					mFailLog = date.msg;
				}
			} catch (Exception e) {
				// TODO: handle exception
				mFailLog = getResources().getString(R.string.loaddata_exception);
				e.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			if ("-1".equals(date.code)) {
				loginWebview();
				return;
			}
			if (!TextUtils.isEmpty(mFailLog)) {
				mLlFail_repayment_schedule.setVisibility(View.VISIBLE);
				mLlFail_repayment_schedule.setOnClickListener(this);
				mLlrepayment_schedule.setVisibility(View.GONE);
				dismissProgressDialog();
				showToast(mFailLog);
			} else {
				mLlFail_repayment_schedule.setVisibility(View.GONE);
				mLlrepayment_schedule.setVisibility(View.VISIBLE);

				PromiseInfo data = date.list;
				// PicLoad.getImage(mIvImage, "", data.guaranteeLicenseUrl);
				mIvImage.setEnabled(false);
				PicLoad.getImage(mIvImage, "", data.guaranteeLicenseUrl, null, new ImageCallback() {
					@Override
					public void imageLoader(Bitmap bitmap, String imageUrl) {
						if (bitmap != null) {
							mIvImage.setImageBitmap(bitmap);
							mIvImage.setVisibility(View.VISIBLE);
							mIvImage.setEnabled(true);
						}
					}
				});

				mTvCommitmentagency.setText(data.guaranteeName);
				mPicUrls.add(data.guaranteeLicenseUrl);
				if (!TextUtils.isEmpty(data.guaranteeLicenseUrl)) {
					mIvImage.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							Intent i = new Intent(RepaymentScheduleActivity.this, BigPicActivity.class);
							i.putExtra("index", 0);
							i.putStringArrayListExtra("picUrls", mPicUrls);
							startActivity(i);
						}
					});
				}
				dismissProgressDialog();
			}

		}

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			new RepaymentScheduleActivityTask(demandId).execute();
			showProgressDialog();
		}
	}
}
